<?
/**
 * Pager class file
 * @package Utilities
 */

/**
 * This class will render a Facebook-style Pager
 * 
 * @package Utilities
 * @author Brad Dougherty <bdougherty@gmail.com>
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License 3.0
 */
class Pager {
	
	public static function getPager($current_page, $page_count, $url, $num_to_show = 5, $page_string = 'page/', $first_last = true) {
		if ($page_count > 1) {
			
			// Make sure we aren't trying to show more pages than there are
			if ($num_to_show > $page_count) {
				$num_to_show = $page_count;
			}
			
			// Which pages should we show?
			$start = max(1, $current_page - floor($num_to_show / 2));
			$end = min($start + ($num_to_show - 1), $page_count);
			$start = $end - ($num_to_show - 1);
			
			// Generate our pager
			$pager = '<ul class="pagerpro">';

			// Show the first page link when applicable
			if (($start > 1) and $first_last and ($page_count > $num_to_show)) {
				$pager .= '<li><a href="'.$url.'page/1/"><fb:intl desc="Link to first page">First</fb:intl></a></li>';
			}
			
			// Show next when applicable
			if ($current_page > 1) {
				$pager .= '<li><a href="'.$url.'page/'.($current_page - 1).'/"><fb:intl desc="Link to previous page">Prev</fb:intl></a></li>';
			}
			
			// Show the pages
			for ($i = $start; $i <= $end; $i++) {
				$pager .= '<li';
				if ($current_page == $i) {
					$pager .= ' class="current"';
				}
				$pager .= '><a href="'.$url.'page/'.$i.'/">'.$i.'</a></li>';
			}
			
			// Show previous when applicable
			if ($current_page < $page_count) {
				$pager .= '<li><a href="'.$url.'page/'.($current_page + 1).'/"><fb:intl desc="Link to next page">Next</fb:intl></a></li>';
			}
			
			// Show the last page link when applicable
			if (($end < $page_count) and $first_last and ($page_count > $num_to_show)) {
				$pager .= '<li><a href="'.$url.'page/'.$page_count.'/"><fb:intl desc="Link to last page">Last</fb:intl></a></li>';
			}
			
			// Close the list
			$pager .= '</ul>';
			
			return $pager;
			
		}
		return false;
	}
	
}

?>